package com.hyfrogx.modules.app.controller.collect;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.BooleanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hyfrogx.common.utils.R;
import com.hyfrogx.common.validator.ValidatorUtils;
import com.hyfrogx.modules.app.annotation.Login;
import com.hyfrogx.modules.app.entity.HyUserCollectEntity;
import com.hyfrogx.modules.app.form.collect.CollectAddForm;
import com.hyfrogx.modules.app.form.collect.CollectUpdateForm;
import com.hyfrogx.modules.app.form.collect.MyCollectQueryForm;
import com.hyfrogx.modules.app.service.HyUserCollectService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * @ClassName ApiCollectController
 * @Description TODO
 * @Author yangjun
 * @date 2019/12/5 15:22
 * @Version 1.0
 **/
@RestController
@RequestMapping("/api/collect")
@Api(value = "ApiCollectController", tags = {"客户端--收藏操作接口"})
public class ApiCollectController {

    @Autowired
    HyUserCollectService hyUserCollectService;

    /**
     * 收藏选中内容
     * @return
     */
    @Login
    @PostMapping(value ="/add")
    public R put(@RequestAttribute(value = "userId") Long userId,@RequestBody CollectAddForm form){
        try {
            ValidatorUtils.validateEntity(form);
            QueryWrapper qw=new QueryWrapper<HyUserCollectEntity>()
                    .eq("vid",form.getVid())
                    .eq("type",form.getType())
                    .eq("uid",userId);
            int count=hyUserCollectService.getBaseMapper().selectCount(qw);
            if(count>0){
                return R.error("不能重复收藏");
            }
            HyUserCollectEntity collectEntity=new HyUserCollectEntity();
            BeanUtil.copyProperties(form,collectEntity);
            collectEntity.setUid(userId);
            collectEntity.setCreateDate(new Date());
            hyUserCollectService.getBaseMapper().insert(collectEntity);
            return R.ok("操作成功");
        } catch (Exception e) {
            return R.error(e.getMessage());
        }
    }

    /**
     * 取消收藏内容
     * @return
     */
    @Login
    @PostMapping("/delete")
    public R delete(@RequestAttribute(value = "userId") Long userId,@RequestBody CollectUpdateForm form){
        try {
            ValidatorUtils.validateEntity(form);
            QueryWrapper qw=new QueryWrapper<HyUserCollectEntity>()
                    .eq("vid",form.getVid())
                    .eq("type",form.getType())
                    .eq("uid",userId);
            if(form.getChannelNum()!=null){
                qw.eq("channnel_num",form.getChannelNum());
            }
            hyUserCollectService.getBaseMapper().delete(qw);
            return R.ok("操作成功");
        } catch (Exception e) {
            return R.error(e.getMessage());
        }
    }

    /**
     * 判断当前视频是否被收藏
     */
    @Login
    @PostMapping("/isCollect")
    public R isCollect(@RequestAttribute(value = "userId") Long userId,@RequestBody CollectUpdateForm form){
        ValidatorUtils.validateEntity(form);
        QueryWrapper qw=new QueryWrapper<HyUserCollectEntity>()
                .eq("vid",form.getVid())
                .eq("type",form.getType())
                .eq("uid",userId);
        if(form.getChannelNum()!=null){
            qw.eq("channnel_num",form.getChannelNum());
        }
        int count=hyUserCollectService.getBaseMapper().selectCount(qw);
        return R.ok().put("result",BooleanUtil.toBoolean(count+""));
    }
}
